﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using log4net.Core;

namespace CYTS.Aoyou.Framework.Log
{
    internal class WebLogImpl : LogImpl, IWebLog
    {
        public WebLogImpl(ILogger logger)
            : base(logger)
        {
        }

        public override void Info(object message)
        {
            HttpContext context = HttpContext.Current;
            if (context != null)
            {
                string url = context.Request.Url == null ? string.Empty : context.Request.Url.ToString();
                if (url.Length > 1000)
                {
                    //URL控制在1000个字符以内
                    url = url.Substring(0, 1000);
                }

                string refer = context.Request.RawUrl == null ? string.Empty : context.Request.RawUrl;
                if (refer.Length > 1000)
                {
                    refer = refer.Substring(0, 1000);
                }
                this.Info(context.Request.UserHostAddress, url, refer, Guid.NewGuid().ToString(), Dns.GetHostName(), context.Request.Url.Host, message);
            }
            else
            {
                this.Info(string.Empty, string.Empty, string.Empty, Guid.NewGuid().ToString(), Dns.GetHostName(), context.Request.Url.Host, message);
            }
        }

        public void Info(string clientIP, string fromUrl, string referUrl, string uniqueID, string serverName, string requestHost, object message)
        {
            if (this.IsInfoEnabled)
            {
                LoggingEvent loggingEvent = new LoggingEvent(typeof(WebLogImpl), Logger.Repository, Logger.Name, Level.Info, message, null);
                loggingEvent.Properties["ClientIP"] = clientIP;
                loggingEvent.Properties["FromUrl"] = fromUrl;
                loggingEvent.Properties["ReferUrl"] = referUrl;
                loggingEvent.Properties["UniqueID"] = uniqueID;
                loggingEvent.Properties["ServerName"] = serverName;
                loggingEvent.Properties["RequestHost"] = requestHost;
                Logger.Log(loggingEvent);
            }
        }

        public override void Error(object message, Exception ex)
        {
            HttpContext context = HttpContext.Current;
            if (context != null)
            {
                string url = context.Request.Url == null ? string.Empty : context.Request.Url.ToString();
                if (url.Length > 1000)
                {
                    url = url.Substring(0, 1000);
                }

                string refer = context.Request.RawUrl == null ? string.Empty : context.Request.RawUrl;
                if (refer.Length > 1000)
                {
                    refer = refer.Substring(0, 1000);
                }
                this.Error(context.Request.UserHostAddress, url, refer, Guid.NewGuid().ToString(), Dns.GetHostName(), context.Request.Url.Host, message, ex);
            }
            else
            {
                this.Error(string.Empty, string.Empty, string.Empty, Guid.NewGuid().ToString(), Dns.GetHostName(), context.Request.Url.Host, message, ex);
            }
        }

        public void Error(string clientIP, string fromUrl, string referUrl, string uniqueID, string serverName, string requestHost, object message, Exception ex)
        {
            if (this.IsErrorEnabled)
            {
                LoggingEvent loggingEvent = new LoggingEvent(typeof(WebLogImpl), Logger.Repository, Logger.Name, Level.Error, message, ex);
                loggingEvent.Properties["ClientIP"] = clientIP;
                loggingEvent.Properties["FromUrl"] = fromUrl;
                loggingEvent.Properties["ReferUrl"] = referUrl;
                loggingEvent.Properties["UniqueID"] = uniqueID;
                loggingEvent.Properties["ServerName"] = serverName;
                loggingEvent.Properties["RequestHost"] = requestHost;
                Logger.Log(loggingEvent);
            }
        }
    }
}
